package com.lovi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovi.domain.TableA;
import com.lovi.domain.TableB;
import com.lovi.mapper.TableBMapper;
import com.lovi.service.TableAService;
import com.lovi.mapper.TableAMapper;
import com.lovi.utils.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Administrator
* @description 针对表【TableA】的数据库操作Service实现
* @createDate 2024-10-12 16:04:09
*/
@Service
@Slf4j
public class TableAServiceImpl extends ServiceImpl<TableAMapper, TableA>
    implements TableAService{
    @Autowired
    private TableAMapper tableAMapper;

    @Autowired
    private TableBMapper tableBMapper;

    @Transactional
//    @Scheduled(cron = "0/10 * * * * ?") // 每10秒执行一次
    public void syncData() {
        // 切换到数据源ds1
        DataSourceContextHolder.setDataSource("ds1");
//        List<TableA> TableAList = tableAMapper.findAll();
//        System.out.println("TableAList: " + TableAList);
//
//        // 切换到数据源ds2
//        DataSourceContextHolder.setDataSource("ds2");
//        for (TableA tableA : TableAList) {
//            TableB tableB = new TableB();
//            tableB.setId(tableA.getId());
//            tableB.setName(tableA.getName());
//            tableBMapper.insert(tableB);
//        }
        List<TableA> tableAS = tableAMapper.selectList(null);
        log.info("ds1:{}",tableAS);

        DataSourceContextHolder.clearDataSource();

        // 清除数据源上下文
//        DataSourceContextHolder.clearDataSource();
    }
}




